define(["dojo/dom", "dojo/_base/window", "dijit/registry", "dojo/json", "dojo/request", "dojo/_base/declare", "dojo/parser", "dijit/_WidgetBase",
	"dijit/_TemplatedMixin", "dijit/layout/ContentPane", "dijit/layout/TabContainer", "dijit/Dialog", 
	"dijit/form/ValidationTextBox", "dijit/form/Button", "dojo/ready", "my/DashboardTab"],
		function(dom, win, registry, JSON, request, declare, parser, _WidgetBase, _TemplatedMixin, ContentPane, TabContainer, 
			Dialog, ValidationTextBox, Button, ready, DashboardTab) {
			
			return declare("my.TabbedDashboards", [_WidgetBase, _TemplatedMixin], {
				templateString:
                    "<div>" + 
					"<div data-dojo-type='dijit/layout/TabContainer' style='height: 100%; width:100%; data-dojo-attach-event='selectChild: createDashboard' data-dojo-attach-point='dashboards'>" +
						"<div data-dojo-type='dijit/layout/ContentPane' title='Dashboard'>" +
                            "<div data-dojo-type='my/DashboardTab'></div>" +
                        "</div>" +
						"<div data-dojo-type='dijit/layout/ContentPane' title='+' data-dojo-attach-point='add'></div>" +
                    "</div>" +
					"</div>",
					
				createDashboard: function(page, animate) {
                    console.log("TEST");
					/*if (page.name == "add") {
						var text = new ValidationTextBox({
							name: "dashName",
							placeholder: "Dashboard Name"}, "dashName");
						var ok = new Button({
							label: "OK",
							onClick: function(){
								var location = page.getParent().getIndexOfChild(page)
								registry.byId("dashboards").addChild(new Dashboard({title: title}));
								dialog.hide();
								registry.byId("dashboards").selectChild(registry.byId("dashboards").getChildren()[location], true);
								widgets(registry.byId("dashboards").getChildren()[location]);
								console.log(registry.byId("dashboards").getChildren()[location]);
							}});
						var dialog = new Dialog({
							title: "Create Dashboard",
							content: [text.domNode, ok.domNode]});
						dialog.show();
						} else {
							page = registry.byId(page);

							if(this.selectedChildWidget != page){
								// Deselect old page and select new one
								var d = this._transition(page, this.selectedChildWidget, animate);
								this._set("selectedChildWidget", page);
								topic.publish(this.id+"-selectChild", page);    // publish

								if(this.persist){
									cookie(this.id + "_selectedChild", this.selectedChildWidget.id);
								}
							}

							return d;
						}*/
							
				},
				
				title: "",
				
				update: function() {
					request.post("dashboards", {
						handleAs: "json"}).then(function (data){
							for (var i = 0; i < data.length; i++) {
								item = data[i];
								registry.byId("dashboards").addChild(new Dashboard({title: item, id: title}));
								
							}
						});
				}

			});
            ready(function() {
                parser.parse();
            });
});
